﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BookStore.Solution.Entities;
using BookStore.Solution.BLL;

namespace BookStore.Solution.Presentation
{
    public partial class frmSupplier : DevComponents.DotNetBar.OfficeForm
    {
        public Supplier NewSupllier = null;
        private SupplierManager manager = new SupplierManager();
        private bool IsAdd;

        public frmSupplier()
        {
            InitializeComponent();
            IsAdd = true;
        }


        public frmSupplier(Supplier entity)
        {
            InitializeComponent();
            IsAdd = false;
            NewSupllier = entity;
            btnSubmit.Text = "Cập nhật";
            Text = "Cập nhật nhà cung cấp có mã " + entity.SupplierID + ":";
            txtSupplierID.Enabled = false;
        }

        private void ResetForm()
        {
            txtAddress.Text = "";
            txtEmail.Text = "";
            txtSupplierID.Text = "";
            txtSupplierName.Text = "";
            txtWebsite.Text = "";
            mtbPhoneNumber.Text = "";
        }

        private void LoadEntity()
        {
            txtAddress.Text = NewSupllier.Address;
            txtEmail.Text = NewSupllier.Email;
            txtSupplierID.Text = NewSupllier.SupplierID;
            txtSupplierName.Text = NewSupllier.SupplierName;
            txtWebsite.Text = NewSupllier.Website;
            mtbPhoneNumber.Text = NewSupllier.PhoneNumber;
        }

        private Supplier GetEntity()
        {
            Supplier entity = new Supplier();
            entity.Address = txtAddress.Text;
            entity.Email = txtEmail.Text;
            entity.SupplierID = txtSupplierID.Text;
            entity.SupplierName = txtSupplierName.Text;
            entity.Website = txtWebsite.Text;
            entity.PhoneNumber = mtbPhoneNumber.Text;
            entity.Status = "Khả dụng";
            return entity;
        }

        private void ResetColor()
        {
            txtSupplierID.Border.BackColor = 
            txtSupplierName.Border.BackColor =
            txtEmail.Border.BackColor =
            mtbPhoneNumber.BackgroundStyle.BorderColor = StyleManager.ColorTint;

        }

        private string CheckValidated()
        {
            string result = "";
            if (txtSupplierID.Text == "")
            {
                result += "Mã NCC rỗng. ";
                txtSupplierID.Border.BackColor = Color.Red;
            }
            if (txtSupplierName.Text == "")
            {
                result += "Mã NCC rỗng. ";
                txtSupplierName.Border.BackColor = Color.Red;
            }
            if (txtEmail.Text != "" && !BookStore.Solution.Utilities.CheckValidated.CheckValidatedEmail(txtEmail.Text))
            {
                result += "Email không đúng dịnh dạng. ";
                txtEmail.Border.BackColor = Color.Red;
            }
            if (mtbPhoneNumber.Text != "" && !(mtbPhoneNumber.Text.Trim().Length >= 9 && mtbPhoneNumber.Text.Trim().Length <= 11))
            {
                result += "Số diện thoại không đúng dịnh dạng. ";
                mtbPhoneNumber.BackgroundStyle.BorderColor = Color.Red;
            }
            return result;
        }

        private bool Add()
        {
            string result = CheckValidated();
            if (result != "")
            {
                MessageBox.Show(result, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            NewSupllier = GetEntity();
            result = manager.Insert(NewSupllier);
            if (result == "Thêm thành công")
            {
                MessageBox.Show(result, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return true;
            }
            MessageBox.Show(result, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return false;
        }

        private bool UpdateSupplier()
        {
            string result = CheckValidated();
            if (result != "")
            {
                MessageBox.Show(result, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            NewSupllier = GetEntity();
            result = manager.Update(NewSupllier);
            if (result == "Cập nhật thành công")
            {
                MessageBox.Show(result, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return true;
            }
            MessageBox.Show(result, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return false;
        }

        private void frmSupplier_Load(object sender, EventArgs e)
        {
            ResetForm();
            if (!IsAdd)
                LoadEntity();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            ResetColor();
            if (IsAdd)
            {
                if (Add())
                    Close();
            }
            else
            {
                if (UpdateSupplier())
                    Close();
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            if (IsAdd)
                ResetForm();
            else
                LoadEntity();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            NewSupllier = null;
            Close();
        }
    }
}